package com.zj.eurekaclient.controller;



import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: zj
 * @Date: 2018/11/21 09:21
 * @Description:
 */
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
@RequestMapping("/service-test")
public class HystrixController {

//    @HystrixCommand(fallbackMethod = "fallback")
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
//    })
    @HystrixCommand(commandProperties = {
            @HystrixProperty( name="circuitBreaker.enabled" ,value = "true"),//设置熔断
            @HystrixProperty( name = "circuitBreaker.requestVolumeThreshold",value = "10"),//配置时间窗口内达到此数量的失败后，进行短路。默认20个
            @HystrixProperty( name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//多久以后开始尝试是否恢复，默认5s
            @HystrixProperty( name = "circuitBreaker.errorThresholdPercentage",value = "60"),//出错百分比阈值，当达到此阈值后，开始短路。默认50%
    })
    @GetMapping("/test1")
    public String clientTest1(@RequestParam("number") Integer number) {

        if (number%2==0) {
            return "success";
        }
        /**
         * 这里先利用restTemplate来传参 , 暂时不用feign
         */
        RestTemplate restTemplate = new RestTemplate(  );
        return restTemplate.getForObject( "http://127.0.0.1:8081/msg",String.class);
        //throw new RuntimeException( "发生一个异常" );
    }
//    private String fallback() {
//        return "太拥挤了，请稍后再试:P";
//    }

    private String defaultFallback() {
        return "默认提示，降级处理";
    }

}
